package com.lsy.android.pingpang.view;

import com.lsy.android.pingpang.R;
import com.lsy.android.pingpang.tool.SharePerenceHelp;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;

public class SettingView extends FrameLayout {

	private static final int[] levelButtonId = { R.id.setting_level1,
			R.id.setting_level2, R.id.setting_level3, R.id.setting_level4,
			R.id.setting_level5 };

	private View view;
	private int lastLevelBtn;
	private int lastModeBtn;

	public SettingView(Context context, final Dialog dialog) {
		super(context);
		view = LayoutInflater.from(context).inflate(R.layout.setting_dialog,
				null);
		addView(view);
		for (int i = 0; i < 5; i++) {
			((CheckBox) view.findViewById(levelButtonId[i]))
					.setOnCheckedChangeListener(levelChanged);
		}
		((CheckBox) view.findViewById(R.id.setting_match_mode))
				.setOnCheckedChangeListener(modeChanged);
		((CheckBox) view.findViewById(R.id.setting_time_mode))
				.setOnCheckedChangeListener(modeChanged);
		view.findViewById(R.id.btn_setting_ok).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
		int level = SharePerenceHelp.getLevel(context);
		((CheckBox) view.findViewById(levelButtonId[level])).setChecked(true);
		switch (SharePerenceHelp.getGameMode(context)) {
		case 0:
			((CheckBox) view.findViewById(R.id.setting_match_mode))
					.setChecked(true);
			break;
		case 1:
			((CheckBox) view.findViewById(R.id.setting_time_mode))
					.setChecked(true);
			break;
		default:
			break;
		}

	}

	private OnCheckedChangeListener modeChanged = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			switch (buttonView.getId()) {
			case R.id.setting_match_mode:
				if (isChecked) {
					SharePerenceHelp.saveGameMode(getContext(), 0);
					view.findViewById(R.id.setting_match_mode_tip)
							.setVisibility(View.VISIBLE);
				} else {
					view.findViewById(R.id.setting_match_mode_tip)
							.setVisibility(View.INVISIBLE);
				}
				break;
			case R.id.setting_time_mode:
				if (isChecked) {
					SharePerenceHelp.saveGameMode(getContext(), 1);
					view.findViewById(R.id.setting_time_mode_tip)
							.setVisibility(View.VISIBLE);
				} else {
					view.findViewById(R.id.setting_time_mode_tip)
							.setVisibility(View.INVISIBLE);
				}
				break;
			default:
				break;
			}
			if (lastModeBtn != 0) {
				if (lastModeBtn == buttonView.getId()) {
					return;
				} else {
					((CheckBox) view.findViewById(lastModeBtn))
							.setChecked(false);
				}
			}
			lastModeBtn = buttonView.getId();
		}
	};

	private OnCheckedChangeListener levelChanged = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			switch (buttonView.getId()) {
			case R.id.setting_level1:
				if (isChecked) {
					SharePerenceHelp.saveLevel(getContext(), 0);
					view.findViewById(R.id.setting_level1_tip).setVisibility(
							View.VISIBLE);
				} else {
					view.findViewById(R.id.setting_level1_tip).setVisibility(
							View.INVISIBLE);
				}
				break;
			case R.id.setting_level2:
				if (isChecked) {
					SharePerenceHelp.saveLevel(getContext(), 1);
					view.findViewById(R.id.setting_level2_tip).setVisibility(
							View.VISIBLE);
				} else {
					view.findViewById(R.id.setting_level2_tip).setVisibility(
							View.INVISIBLE);
				}
				break;
			case R.id.setting_level3:
				if (isChecked) {
					SharePerenceHelp.saveLevel(getContext(), 2);
					view.findViewById(R.id.setting_level3_tip).setVisibility(
							View.VISIBLE);
				} else {
					view.findViewById(R.id.setting_level3_tip).setVisibility(
							View.INVISIBLE);
				}
				break;
			case R.id.setting_level4:
				if (isChecked) {
					SharePerenceHelp.saveLevel(getContext(), 3);
					view.findViewById(R.id.setting_level4_tip).setVisibility(
							View.VISIBLE);
				} else {
					view.findViewById(R.id.setting_level4_tip).setVisibility(
							View.INVISIBLE);
				}
				break;
			case R.id.setting_level5:
				if (isChecked) {
					SharePerenceHelp.saveLevel(getContext(), 4);
					view.findViewById(R.id.setting_level5_tip).setVisibility(
							View.VISIBLE);
				} else {
					view.findViewById(R.id.setting_level5_tip).setVisibility(
							View.INVISIBLE);
				}
				break;
			}
			if (lastLevelBtn != 0) {
				if (lastLevelBtn == buttonView.getId()) {
					return;
				} else {
					((CheckBox) view.findViewById(lastLevelBtn))
							.setChecked(false);
				}
			}
			lastLevelBtn = buttonView.getId();
		}
	};

}
